package thread;

import java.util.Scanner;

public class Demo17 {
    public static void main(String[] args) {
        Object locker = new Object();
        Thread t1 = new Thread(()->{
            try{
                synchronized (locker){
                    System.out.println("t1 wait...");
                    locker.wait();
                    System.out.println("t1 stop wait...");
                }
            }catch (InterruptedException e){
                throw new RuntimeException();
            }
        });

        Thread t2 = new Thread(()->{
            try{
                synchronized (locker){
                    System.out.println("t2 wait...");
                    locker.wait();
                    System.out.println("t2 stop wait...");
                }
            }catch (InterruptedException e){
                throw new RuntimeException();
            }
        });

        Thread t3 = new Thread(()->{
            Scanner scanner = new Scanner(System.in);
            System.out.println("输入任意内容以唤醒其他线程：");
            scanner.next();
            synchronized (locker){
                //多个wait，只有一个notify，会随机唤醒一个线程
                //多次调用notify，就会唤醒多个线程
                locker.notify();
                locker.notify();
                // 唤醒所有wait，会出现锁竞争的问题，一个加上锁，另一个阻塞等待
                // locker.notifyAll();
            }
        });
        t1.start();
        t2.start();
        t3.start();
    }
}
